<?php

  class Test {

      var $data;

      function getJson () {
          return my_json_encode( $this->data );
      }

      function setJson ( $json ) {
          $this->data = my_json_decode( $json );
      }

      /*
        function parse ( $filename ) {
        $this->data = null;
        if ( ($handle = fopen( $filename, "r" )) !== FALSE ) {
        $row = 0;
        while ( ($data = fgetcsv( $handle, 1000, "," )) !== FALSE ) {
        if ( $row++ == 0 )
        continue;
        $id = $row - 1;
        $task = new Task( $data, $id );
        if ( $task->isCorrect() )
        $this->data[$id] = $task;
        }
        fclose( $handle );
        }
        } */

      function getId ( $id ) {
          
      }

      function getTasks ( $ids = null ) {
          if ( $ids === null )
              $ids = $this->data['task_ids'];
          $where = array2string( $ids, '\'', ' OR ', '`id` = ' );
          $query = <<<STR
SELECT * 
FROM  `cont_questions` 
WHERE  {$where}
STR;
          $res = mysql_query( $query );
          $tasks = array( );
          while ( ($row = mysql_fetch_assoc( $res )) !== false ) {
              $task = new RealTask();
              $task->load( $row );
              $tasks[] = $task;
          }
          return $tasks;
      }

      function getRandom ( $spec_id ) {
          $this->data['spec_id'] = $spec_id;
          $this->data['task_ids'] = $this->getRandomTaskId( $spec_id );
      }

      function getRandomTaskId ( $spec_id ) {
          $SPEC = array(
              1 => array(
                  'name' => 'Java программист',
                  'modules' => array(
                      '1' => '5',
                  )
              ),
              2 => array(
                  'name' => 'PHP программистик',
                  'modules' => array(
                      '2' => '5',
                  )
              ),
              3 => array(
                  'name' => 'Средненький програмистик',
                  'modules' => array(
                      '1' => '2',
                      '2' => '3',
                  )
              ),
          );
          $spec_id = 3;
          $spec = $SPEC[$spec_id];

          $module_ids = array_keys( $spec['modules'] );
          $module_id = array2string( $module_ids, '\'', ' OR ', '`module_id` = ' );
          $MAX_QUESTIONS = 5;
          $MAX_TIME = 300;
          $query = <<<STR
SELECT * 
FROM  `cont_questions` 
WHERE  {$module_id}
ORDER BY rand()
LIMIT {$MAX_QUESTIONS}
STR;
          //var_dump( $query );
          $res = mysql_query( $query );
          $random_ids = array( );
          $max_count = array_sum( $spec['modules'] );
          while ( ($row = mysql_fetch_assoc( $res )) !== false ) {
              $task = new RealTask();
              $task->load( $row );
              if ( $spec['modules'][$task->getModuleId()] <= 0 )
                  continue;
              $total_time += $task->getTime();
              $max_count -= 1;
              $random_ids[] = $task->getId();

              if ( $max_count <= 0 )
                  break;
              if ( $total_time >= $MAX_TIME )
                  break;
          }
          return $random_ids;
      }

      function getByIds ( $ids ) {
          foreach ( $ids as $id ) {
              $ret[] = $this->data[$id];
          }
          return $ret;
      }

      function getRate () {
          return $this->data['rate'];
      }

      function getCash () {
          return $this->data['cash'];
      }

      function setAnswers ( $allanswers ) {
          $tasks = $this->getTasks();
          $sum_weight = 0;
          foreach ( $tasks as $task ) {
              $answers = $allanswers[$task->getId()];
              if ( count( $answers ) <= 0 ) {
                  continue;
              }
              $sum_weight +=$task->getWeight();
              $sum_rate += $task->rateAnswer( $answers );
          }
          $this->data['rate'] = $sum_rate / $sum_weight;
          $this->data['cash'] = $this->data['rate'] * 15000;
      }

  }
?>
